﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DemoOHClaudia
{
    using Microsoft.Research.Kinect;
    using Microsoft.Research.Kinect.Audio;
    using Microsoft.Research.Kinect.Nui;

    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        private Runtime runtime;
        private Image claudia;
        public MainWindow()
        {
            InitializeComponent();

            claudia = this.Resources["ClaudiaImage"] as Image;
            runtime = new Runtime();
            runtime.Initialize(RuntimeOptions.UseColor | RuntimeOptions.UseSkeletalTracking);

            runtime.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480, ImageType.Color);
            runtime.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(runtime_VideoFrameReady);
            runtime.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(runtime_SkeletonFrameReady);

        }

        private int currentTrackingId = -1;
        private double claudiaHeight;
        private double claudiaWidth;

        void runtime_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            const double claudiaHight = 719;
            const double claudiaRation = 165.0 / 176.0;
            if (ARCanvas.Children.Count == 0)
            {
                Canvas.SetTop(claudia, 0);
                Canvas.SetLeft(claudia, 0);
                claudia.Opacity = 0;
                ARCanvas.Children.Add(claudia);
            }
            var frame = e.SkeletonFrame;
            foreach (var data in frame.Skeletons)
            {
                if (data.TrackingState == SkeletonTrackingState.Tracked)
                {
                    if (currentTrackingId == -1)
                    {
                        currentTrackingId = data.TrackingID;
                        claudia.Opacity = 1;
                    }
                    else
                    {
                        if (currentTrackingId != data.TrackingID)
                        {
                            currentTrackingId = -1;
                            claudia.Opacity = 0;
                        }
                        else
                        {
                            double by = (1.0 - data.Joints[JointID.AnkleRight].Position.Y) * ARCanvas.ActualHeight / 2.0;
                            double ty = (1.0 - data.Joints[JointID.Head].Position.Y) * ARCanvas.ActualHeight / 2.0;
                            double rx = (1.0 + data.Joints[JointID.HandRight].Position.X) * ARCanvas.ActualWidth / 2.0;
                            double cx = (1.0 + data.Joints[JointID.Spine].Position.X) * ARCanvas.ActualWidth / 2.0;

                            double myHight = by - ty;
                            double scale = myHight * claudiaRation / claudiaHight;
                            tbMemo.Text = String.Format("CH={0:0.00},CW={1:0.00}",claudia.ActualHeight,claudia.ActualWidth);
                            ScaleTransform sform = new ScaleTransform(scale, scale);
                            claudia.RenderTransform = sform;

                            double claudiaTop = by - myHight * claudiaRation;
                            double claudiaLeft = rx + (rx - cx) * 2;
                            Canvas.SetTop(claudia, claudiaTop);
                            Canvas.SetLeft(claudia, claudiaLeft);
                        }
                    }
                }
                else
                {
                    if (currentTrackingId == data.TrackingID)
                    {
                        currentTrackingId = -1;
                        claudia.Opacity = 0;
                    }
                }
            }
        }

        void runtime_VideoFrameReady(object sender, ImageFrameReadyEventArgs e)
        {
            PlanarImage image = e.ImageFrame.Image;
            videoFrameImage.Source = BitmapImage.Create(image.Width, image.Height
                , 96, 96, PixelFormats.Bgr32, null, image.Bits, image.Width * image.BytesPerPixel);
        }
    }
}
